package com.sleepsocial.persistency.entity.oauth;

import java.util.Date;
import java.util.List;

import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.sleepsocial.persistency.PMF;
import com.sleepsocial.persistency.entity.user.Gender;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class OauthUserProfile {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    protected Key           key;

    @Persistent
    protected String        accessToken;

    @Persistent
    protected String        secret;

    @Persistent
    protected String        email;

    @Persistent
    protected String        name;

    @Persistent
    protected String        displayName;

    @Persistent
    protected String        profileUrl;

    @Persistent
    protected Gender        gender;

    @Persistent
    protected Date          birthDate;

    @Persistent
    protected OauthProvider provider;

    @Persistent
    protected String        photoUrl;

    @Persistent
    protected String        address;

    @Persistent
    protected String        identifier;

    @SuppressWarnings("unchecked")
    public static OauthUserProfile getByEmail(String email, OauthProvider provider) {
        Query q = PMF.getPersistenceManager().newQuery(OauthUserProfile.class);
        q.setFilter("email == \"" + email + "\"");
        List<OauthUserProfile> profiles = (List<OauthUserProfile>) q.execute();
        if (profiles.size() > 0) {
            for (OauthUserProfile profile : profiles) {
                if (profile.getProvider().equals(provider)) {
                    return profile;
                }
            }
            return null;
        } else {
            return null;
        }
    }

    public OauthUserProfile(String email) {
        this.email = email;
    }

    public OauthUserProfile(String access_token, String secret) {
        this.accessToken = access_token;
        this.secret = secret;
    }

    public String getAccessToken() {
        return accessToken;
    }

    /**
     * to make sure only 1 oauthUserProfile of the same provider is added to a
     * OauthUserProfile collection
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        OauthUserProfile other = (OauthUserProfile) obj;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (provider == null) {
            if (other.provider != null)
                return false;
        } else if (!provider.equals(other.provider))
            return false;
        return true;
    }

    public String getAddress() {
        return address;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public String getDisplayName() {
        return displayName;
    }

    public String getEmail() {
        return email;
    }

    public Gender getGender() {
        return gender;
    }

    public String getIdentifier() {
        return identifier;
    }

    public Key getKey() {
        return key;
    }

    public String getName() {
        return name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public String getProfileUrl() {
        return profileUrl;
    }

    public OauthProvider getProvider() {
        return provider;
    }

    public String getSecret() {
        return secret;
    }

    public boolean isSameUser(OauthUserProfile profile) {
        return profile.email.equals(this.email);
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }

    public void setProfileUrl(String profileUrl) {
        this.profileUrl = profileUrl;
    }

    public void setProvider(OauthProvider provider) {
        this.provider = provider;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    @Override
    public String toString() {
        return this.provider.name() + "|" + this.email + "|" + this.name + "|" + this.identifier;
    }

}
